package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.CrmCustomerInfoAddDTO;
import org.jsola.hr.dto.CrmCustomerInfoUpdateDTO;
import org.jsola.hr.query.CrmCustomerInfoQuery;
import org.jsola.hr.service.ICrmCustomerInfoService;
import org.jsola.hr.vo.CrmCustomerInfoListVO;
import org.jsola.hr.vo.CrmCustomerInfoVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 客户详情表
 *
 * @author zhr
 */
@RestController("hrCrmCustomerInfoController")
@RequestMapping(HrConstants.API_PATH + "/v1/crmCustomerInfos")
public class CrmCustomerInfoController implements ICrmCustomerInfoControllerAPI {

    @Autowired
    private ICrmCustomerInfoService crmCustomerInfoService;

    @Override
    @GetMapping("/queryCustomerInfo")
    public Result<CrmCustomerInfoVO> queryCustomerInfo(@Valid CrmCustomerInfoQuery crmCustomerInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增客户详情表权限校验

        return Result.success(
                crmCustomerInfoService.queryCustomerInfo(crmCustomerInfoQuery, tokenUser)
        );
    }

    @Override
    @PostMapping("/saveCustomer")
    public Result<CrmCustomerInfoVO> saveCustomer(@Valid @RequestBody CrmCustomerInfoAddDTO crmCustomerInfoAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增客户详情表权限校验

        return Result.success(
                crmCustomerInfoService.saveCustomer(crmCustomerInfoAddDTO, tokenUser)
        );
    }

    @Override
    @GetMapping("/getCustomerPage")
    public Result<Page<CrmCustomerInfoListVO>> getCustomerPage(@Valid CrmCustomerInfoQuery crmCustomerInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增客户详情表权限校验

        return Result.success(
                crmCustomerInfoService.getCustomerPage(crmCustomerInfoQuery, tokenUser)
        );
    }

    @Override
    @PostMapping("/batchDistribution")
    public Result<Integer> batchDistribution(@Valid @RequestBody CrmCustomerInfoUpdateDTO crmCustomerInfoUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增客户详情表权限校验

        return Result.success(
                crmCustomerInfoService.batchDistribution(crmCustomerInfoUpdateDTO, tokenUser)
        );
    }

    @Override
    @PostMapping
    public Result<CrmCustomerInfoVO> save(@Valid @RequestBody CrmCustomerInfoAddDTO crmCustomerInfoAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增客户详情表权限校验

        return Result.success(
                crmCustomerInfoService.save(crmCustomerInfoAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody CrmCustomerInfoUpdateDTO crmCustomerInfoUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改客户详情表权限校验

        int count = crmCustomerInfoService.update(crmCustomerInfoUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{crmCustomerInfoId}")
    public Result<Integer> delete(@PathVariable String crmCustomerInfoId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除客户详情表权限校验

        int count = crmCustomerInfoService.deleteByIds(tokenUser, crmCustomerInfoId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] crmCustomerInfoIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除客户详情表权限校验

        int count = crmCustomerInfoService.deleteByIds(tokenUser, crmCustomerInfoIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{crmCustomerInfoId}")
    public Result<CrmCustomerInfoVO> findById(@PathVariable String crmCustomerInfoId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看客户详情表权限校验

        return Result.success(
            crmCustomerInfoService.selectById(crmCustomerInfoId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<CrmCustomerInfoListVO>> listAll(@Valid CrmCustomerInfoQuery crmCustomerInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看客户详情表权限校验

        return Result.success(
            crmCustomerInfoService.select(crmCustomerInfoQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<CrmCustomerInfoListVO>> page(@Valid CrmCustomerInfoQuery crmCustomerInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            crmCustomerInfoService.selectPage(crmCustomerInfoQuery, tokenUser.getSiteId())
        );
    }


}

